package com.example.springstudyvue.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springstudyvue.entity.User;
import com.example.springstudyvue.mapper.UserMapper;
import com.example.springstudyvue.result.Result;
import com.example.springstudyvue.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author linKe
 * @since 2021-11-26
 */
@ApiSupport(author = "linKe 开发的类接口")
@Api(value="用户接口",tags="userApi")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    @ApiOperationSupport(author = "注册用户接口")
    @ApiOperation(value="根据用户注册信息录入",notes="返回操作结果")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody User user) {
        if( userService.register(user)){
            return Result.success(userService.register(user));
        }else {
            return new Result<>("400","用户名已存在，请重试！",userService.register(user));
        }
    }

    @ApiOperation(value="根据用户ID获取",notes="返回用户信息")
    @GetMapping("/{id}")
    public Result<User> findUser(@PathVariable("id") Long userId) {
        return Result.data(userService.findUser(userId));
    }

    @ApiOperationSupport(author = "linKe 的新增用户接口")
    @ApiOperation(value="根据用户信息新增",notes="返回操作结果")
    @PostMapping()
    public Result<Boolean> addUser(@RequestBody User user) {
        return Result.success(userService.addUser(user));
    }

    @ApiOperation(value="根据用户更新数据",notes="返回操作结果")
    @PutMapping()
    public Result<Boolean> updateUser(@RequestBody User user) {
        return Result.success(userService.updateUser(user));
    }

    @ApiOperation(value="根据用户ID删除",notes="返回操作结果")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable("id") Long userId) {
        return Result.success(userService.deleteUser(userId));
    }

    @ApiOperation(value="根据用户信息登录",notes="返回用户信息")
    @PostMapping("/login")
    public Result<User> login(@RequestBody User user) {
        if( userService.login(user) == null ){
            return new Result<>("400","用户名或者密码错误！",userService.login(user));
        }else {
            return Result.data(userService.login(user));
        }

    }




    @ApiOperation(value = "一对多查询，带出用户所拥有的书籍信息",notes="作为展示的数据，这样会被列表信息所折叠，有点不合理！返回用户信息")
    @GetMapping
    public Result<Page<User>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(name = "search",defaultValue = "") String search) {
        return  Result.data(userService.findPage(pageNum, pageSize, search));
    }








}
